package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.springcloud.consumer.service.StudentRibbonService;
import com.ming.springcloud.consumer.util.ResponseEntity;
import com.ming.springcloud.consumer.vo.Student;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/student_ribbon")
public class StudentRibbonController {
    @Resource
    private StudentRibbonService studentRibbonService;

    @RequestMapping(value = "/user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUser() {

        String userJSON = studentRibbonService.getUser();
        System.out.println(userJSON);
        return userJSON;
    }

    @RequestMapping(value = "/findAllStudents", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findAllStudent(){
        String userJSON = studentRibbonService.findAllStudents();
     //   System.out.println(userJSON);
        return ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "/findStudentsById/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findStudentsByIds(@PathVariable("id")Integer id){
    //    System.out.println("id="+id);
        String userJSON = studentRibbonService.findStudentsById( id);
     //   System.out.println(userJSON);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String stuName=object.get("stuName").toString();
        String stuNum=object.get("stuNum").toString();
        String stuClass=object.get("stuClass").toString();
        String serviceName=object.get("serviceName").toString();
        String ids=object.get("id").toString();

        Student student=new Student();
        student.setId(Integer.parseInt(ids));
        student.setServiceName(serviceName);
        student.setStuClass(stuClass);
        student.setStuName(stuName);
        student.setStuNum(stuNum);
     //   System.out.println("学生信息="+student);
        return  ResponseEntity.success().add("user",student);
    }
}
